// system.h 
//	All global variables used in Nachos are defined here.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef SYSTEM_H
#define SYSTEM_H

#include "copyright.h"
#include "utility.h"
#include "thread.h"
#include "scheduler.h"
#include "interrupt.h"
#include "stats.h"
#include "timer.h"
#include "synch.h"


// Initialization and cleanup routines
extern void Initialize(int argc, char **argv); 	// Initialization,
						// called before anything else
extern void Cleanup();				// Cleanup, called when
						// Nachos is done.

extern Thread *currentThread;			// the thread holding the CPU
extern Thread *threadToBeDestroyed;  		// the thread that just finished
extern Scheduler *scheduler;			// the ready list
extern Interrupt *interrupt;			// interrupt status
extern Statistics *stats;			// performance metrics
extern Timer *timer;				// the hardware alarm clock

#ifdef USER_PROGRAM
//-------------------------------------------------------------------
//               nachos-team3 addition
//-------------------------------------------------------------------
#include "machine.h"

extern Machine* machine;	// user program memory and registers


//defines

#define MAX_LOCKS 	500
#define MAX_CVS   	1000
#define MAX_PROCESS 50
#define MAX_MVS   	500
#define MAX_MVARRAY	50


//BitMap for Main Memory
extern BitMap *phyBitMap;
//Make sure to acquire this lock when changing phyBitMap or pagetable
extern Lock *phyBitMapLock;

//user lock object
typedef struct lockObj
{
	Lock *lock;
	AddrSpace *addrSpace;
	bool isDeleted;
	bool isToBeDeleted;
	int lockCounter;
}lockObjTemplate;
//Array of user locks
typedef struct lockArray
{
	lockObjTemplate locks[MAX_LOCKS];
	BitMap *lockBitMap;

	lockArray(){
		lockBitMap = new BitMap(MAX_LOCKS);
		for(int i=0; i<MAX_LOCKS; i++){
			locks[i].lock=NULL;
			locks[i].addrSpace=NULL;
			locks[i].isDeleted=false;
			locks[i].isToBeDeleted=false;
			locks[i].lockCounter = 0;
		}
	}
}lockArrayTemplate;
//user CV object
typedef struct cvObj
{
	Condition *cv;
	AddrSpace *addrSpace;
	bool isDeleted;
	bool isToBeDeleted;
	int cvCounter;
}cvObjTemplate;
//Array of user CV's
typedef struct cvArray
{
	cvObjTemplate cvs[MAX_CVS];
	BitMap *cvBitMap;

	cvArray(){
		cvBitMap = new BitMap(MAX_CVS);
		for(int i=0; i>MAX_CVS; i++){
			cvs[i].cv=NULL;
			cvs[i].addrSpace=NULL;
			cvs[i].isDeleted=false;
			cvs[i].isToBeDeleted=false;
			cvs[i].cvCounter = 0;
		}
	}
}cvArrayTemplate;


typedef struct processTableStruct
{
	AddrSpace *addrSpace;
	int activeThreadCntr;
	int totalThreads;
	int threadCntr;
	
}processTableTemplate;

extern processTableTemplate processTable[MAX_PROCESS];
extern Lock *processTableLock;
extern BitMap *processTableBitMap;

extern lockArrayTemplate kernelLockTable;
extern Lock *kernelLockTableLock;
extern cvArrayTemplate kernelCVTable;
extern Lock *kernelCVTableLock;
//-------------------------------------------------------------------
//               nachos-team3 addition
//-------------------------------------------------------------------
#endif

#ifdef FILESYS_NEEDED 		// FILESYS or FILESYS_STUB 
#include "filesys.h"
extern FileSystem  *fileSystem;
#endif

#ifdef FILESYS
#include "synchdisk.h"
extern SynchDisk   *synchDisk;
#endif

#ifdef NETWORK
#include "post.h"
extern PostOffice* postOffice;
/* mailbox counter for assigning mailBox id's to different processes*/
extern int mailBoxCounter;
//bool to store if the testing is for project4 part1 or part2
extern bool proj4part1;
//variable to hold the number of servers
extern int noOfServers;

#define CreateLock_id			1
#define DestroyLock_id  		2
#define CreateCondition_id  	3
#define DestroyCondition_id 	4
#define Acquire_id  			5
#define Release_id				6
#define Wait_id 				7
#define Signal_id 				8
#define Broadcast_id 			9
#define CreateMV_id				10
#define SetMV_id				11
#define GetMV_id				12
#define DestroyMV_id			13
#define ListAppend_id			14
#define ListRemove_id			15
#define	IsListEmpty_id			16
#define TimestampMsg_id			17
#define	ServerForwardMsg_id		18


#define MAX_NO_OF_SERVERS		5
#define ServerMachineId			0
#define ServerMailBoxId			0
#define PositiveReply			1
#define NegativeReply			-1


#endif

#endif // SYSTEM_H
